<?php

require_once('gbif.general.php');
require_once('gbif.meta.php');

/**
 * Class Checklist
 * Functionalities common to the templates
 */
class Checklist extends General{
	public $meta;

	public $checklistFilename = 'checklist.csv';
	public $vernacularFilename = 'vernacular.csv';
	public $distributionFilename = 'distribution.csv';

	public $speciesIndex = 1;
	public $synIndex = 1;
	public $vernacularIndex = 1;
	public $distributionIndex = 1;

	public $checklistOutputHeaderList = array(
			'id'
		,	'parentNameUsageID'
		,	'acceptedNameUsageID'
		,	'scientificName'
		,	'scientificNameAuthorship'
		,	'nomenclaturalCode'
		,	'taxonRank'
		,	'nameAccordingTo'
		,	'taxonRemarks'
	);

	public $distributionHeaderList = array(
			'taxonID'
		,	'locality'
		,	'threatStatus'
		, 'yearEvaluated'
		,	'nativeStatus'
	);

	public $vernacularHeaderList = array(
			'taxonID'
		, 'vernacularName'
	);


	public function __construct( $file = NULL ) {
		parent::__construct($file);
		$this->meta = new Meta;
	}

	public function openFile() {
		if (!$this->fileOpen) return $this->openExcel();
		return true;
	}

	public function getIndexes() {
		return( array(
				"species" => $this->speciesIndex
			,	"synonyms" => $this->synIndex
			,	"vernacular" => $this->vernacularIndex
			,	"distribution" => $this->distributionIndex
		));
	}

	public function getList( $list, $delimiter = '|' ) {
		return( explode($delimiter, $list) );
	}

	public function writeDistributionData( $fp, $parentID, $countries, $threatStatus, $yearEvaluated, $nativeStatus ) {
		$countries = $this->getList( $countries['countries'], $countries['delimiter'] );

		if (is_array($countries)) {
			foreach( $countries as $country){
				$this->distributionIndex++;
//				if (trim($country) != '') {
					fputcsv($fp, array( 
							$parentID
						, trim($country)
						, trim($threatStatus)
						, trim($yearEvaluated)
						, trim($nativeStatus)
					));
//				}
			}
		} else {
//			if ($countries != "") {
				$this->distributionIndex++;
				fputcsv($fp, array( 
						$parentID
					, trim($countries)
					, trim($threatStatus)
					, trim($yearEvaluated)
					, trim($nativeStatus)
				));
//			}
		}
	}

	public function writeSynonymData( $fp, $parentID, $synonyms ) {
		
		foreach( $this->getList($synonyms) as $synonym ) {
#V1.1 ADD DO WE call GBIF NAME PARSER?
			if(trim($synonym) != '') {
				$tmpSynonym = array(
						"syn" . $this->synIndex++
					,	""
					,	$parentID
					,	$synonym
					,	""
					,	""
					,	""
				);
				fputcsv($fp, $tmpSynonym );
			}
		}
	}

	public function writeVernacularData( $fp, $parentID, $commonName ) {
		foreach($this->getList($commonName) as $common) {
			$this->vernacularIndex++;
			$common = trim($common);
			fputcsv($fp, array( 
					$parentID
				,	$common
			));
		}
	}

	public function vernacularDataExist() {
		if ($this->vernacularIndex == 1) {
			return( false );
		} else {
			return( true );
		}
	}

	public function distributionDataExist() {
		if ($this->distributionIndex == 1) {
			return( false );
		} else {
			return( true );
		}
	}

	public function getFilenames() {
		$list = array( $this->checklistFilename );
		if ($this->vernacularDataExist()) {
			array_push( $list, $this->vernacularFilename );
		}
		if ($this->distributionDataExist()) {
			array_push( $list, $this->distributionFilename );
		}
		return( $list );
	}

}
?>